package com.springboot.testspringboot;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.springboot.testspringboot.bean.Result;
import com.springboot.testspringboot.utils.HttpUtil;
import com.springboot.testspringboot.utils.OKHttpUitls;
import com.springboot.testspringboot.utils.ToastUtils;

import org.w3c.dom.Text;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivityTag";
    //    private String url = "http://192.168.1.131:9090/hello";

    private EditText mUserNameEt;
    private EditText mPswdEt;
    private HttpUtil mHttpUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mHttpUtil = HttpUtil.getInstance();

        initView();
    }

    private void initView() {

        mUserNameEt = findViewById(R.id.username_et);
        mPswdEt = findViewById(R.id.password_et);
        TextView mRegistTv = findViewById(R.id.regist_tv);
        mRegistTv.setOnClickListener(this);
        TextView mLoginTv = findViewById(R.id.login_tv);
        mLoginTv.setOnClickListener(this);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        ToastUtils.destory();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.regist_tv:

                //注册接口
                getRequest("/regist");

                break;
            case R.id.login_tv:

                //登录接口
                getRequest("/login");

                break;
        }
    }

    private void getRequest(String param) {
        String URL = "http://192.168.1.131:9090/user" + param;
        String mUserName = mUserNameEt.getText().toString().trim();
        String mPswd = mPswdEt.getText().toString().trim();
        if (TextUtils.isEmpty(mUserName) || TextUtils.isEmpty(mPswd)) {
            ToastUtils.showInMainThread(MainActivity.this, "请输入用户名和密码");
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("username", mUserName);
        map.put("password", mPswd);
        mHttpUtil.postDataAsynToNet(URL, map, new HttpUtil.MyNetCall() {
            @Override
            public void success(Call call, Response response) throws IOException {
                if (response != null) {
                    Gson gson = new Gson();
                    String string = response.body().string();
                    Log.d(TAG, "success: " + string);
                    Result result = gson.fromJson(string, Result.class);
                    String msg = result.getMsg();
                    ToastUtils.showInThread(MainActivity.this, msg);
                }
            }

            @Override
            public void failed(Call call, IOException e) {
                Log.d(TAG, "failed: " + e);
                ToastUtils.showInThread(MainActivity.this, e.getMessage());
            }
        });
    }
}